package com.glory.brepda.activity.check;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.QCCheckAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.QCCheckBean;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * QC检验
 */
public class QCTaskGeneratorActivity extends BaseActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;

    @BindView(R.id.quipmentId)
    FreeUI_ClearEditText quipmentId;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_qctaskgenerator;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {

        setMainLayout(llMain);
        tvTitle.setText("QC任务生成");
        tvBaseSubmit.setText("提交");
        tvBaseSubmit.setVisibility(View.VISIBLE);

    }

    @Override
    protected void initData() {


        tvBaseSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(checkIsNull(etLotId.getText())||checkIsNull(quipmentId.getText())   ){
                    showToast("批次号和设备编号不能为空", false);
                    return;
                }
                submit();
            }
        });

    }


    public boolean checkIsNull(Object str){
        return str == null || StringUtils.isEmpty(str.toString()) || StringUtils.isBlank(str.toString());
    }

    private void submit(){

        FreeUI_EntityCallBack<BaseResponseBean<QCCheckBean>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<QCCheckBean>>
                (new TypeToken<BaseResponseBean<QCCheckBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<QCCheckBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    setResult(Activity.RESULT_OK);
                    finish();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<QCCheckBean>> response) {
                super.onError(response);
                loadError(response.getException(), "QCTaskGenerator");
            }
        };
        List<String> equipment = new ArrayList<>();
        equipment.add(getText(quipmentId));
        JsonObject map = new JsonObject();
        map.addProperty("LOT_ID", getText(etLotId));
        map.add("EQUIPMENTID", new JsonParser().parse(new Gson().toJson(equipment)).getAsJsonArray());//设备ID
        NetUtils.requestPostNet("IF.QCTASKGENERATORBYEQUIDANDLOTID", "Common", QCTaskGeneratorActivity.this, map, callback);

    }
}